package com.dd.ent.car.ui.detail;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.dd.ent.car.R;
import com.dd.ent.car.common.bean.video.Video;
import com.dd.ent.car.common.player.MediaPlayerAcceptor;
import com.dd.ent.car.common.player.MediaPlayerManager;
import com.dd.ent.car.common.player.MediaPlayerState;
import com.dd.ent.car.common.player.MediaPlayerStateChangeListener;
import com.dd.ent.car.common.player.ThunderMediaPlayer;
import com.dd.ent.car.common.player.VideoInfo;
import com.dd.ent.car.ui.home.templet.video.VideoItemView;

/**
 * Created by dengqu on 2016/8/29.
 */
public class DetailVideoHeaderView extends FrameLayout implements View.OnClickListener{
    private VideoItemView videoItemView;
    private Video video;

    public DetailVideoHeaderView(Context context) {
        super(context);
        init(context);
    }

    public DetailVideoHeaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        View root = LayoutInflater.from(context).inflate(R.layout.detail_video_header_view, this, true);
        videoItemView = (VideoItemView) root.findViewById(R.id.video_view);
        initListener();
    }

    private void initListener(){
        videoItemView.setOnClickListener(this);
    }

    public void updateData(Video video) {
        this.video = video;
        videoItemView.refreshData(0, video);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.video_view:
                doPlay();
                break;
            default:
                break;
        }
    }

    private void doPlay() {
        final ThunderMediaPlayer videoPlayer = MediaPlayerManager.getInstance().applyMediaPlayer(getContext(), new MediaPlayerAcceptor() {
            @Override
            public void onPlayerAccept(ThunderMediaPlayer mediaPlayer) {
                mediaPlayer.addContainerTo(videoItemView.getPlayContainer());
                mediaPlayer.addStateChangeListener(mStateChangeListener);
                mediaPlayer.setListPosition(0);
            }

            @Override
            public void onPlayerRelease(ThunderMediaPlayer mediaPlayer) {
                mediaPlayer.removeStateChangeListener(mStateChangeListener);
                mediaPlayer.removeSelfContainer();
            }
        }, MediaPlayerManager.PlayerTag.SHORT_VIDEO_DETAIL);
        VideoInfo videoInfo = new VideoInfo(video.getUuid(),video.getPlayUrl(),video.getTitle());
        videoPlayer.play(videoInfo);
    }

    private MediaPlayerStateChangeListener mStateChangeListener = new MediaPlayerStateChangeListener() {
        @Override
        public void onStateChange(MediaPlayerState oldState, MediaPlayerState newState) {
            switch (newState) {
                case INITIALIZED:
                case PREPARING:
                    break;
                case STARTED:
                    break;
            }
        }
    };
}
